import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import {AliyunOssOutputUrl} from '../../../../../config/api';
import {HomeConfigService} from '../../../../lib/service/admin/home-config.service';
import {AdOssUploadService} from '../../../../lib/service/ad-oss-upload.service';
import { AddHomeConfig } from '../../config';

@Component({
  selector: 'app-add-config',
  templateUrl: './add-config.component.html',
  styleUrls: ['./add-config.component.scss'],
  providers: []
})
export class AddConfigComponent implements OnInit {

  addHomeConfig = new AddHomeConfig;
  aliyunossoutputurl = AliyunOssOutputUrl;
  hasUploadImg = true;

  type: string;
  message: string;
  isShow = false;
  img_file: any;
  img_name: string;
  img_url: string;
  ewm_file: any;
  ewm_name: string;
  ewm_url: string;

  constructor(private homeConfigService: HomeConfigService,
              private adOssUploadService: AdOssUploadService,
              private router: Router) { }

  ngOnInit() {
  }

  // 上传图片
  uploadImg($event: any, imgName: string) {
    const file = $event.path[0].files[0];
    const myReader = new FileReader();
    const self = this;
    const suffix = file.name.replace(/.+\./, '').toLowerCase();
    if (imgName === 'logo') {
      this.hasUploadImg = true;
      this.img_file = file;
      this.img_name = imgName + '.' + suffix;
      myReader.readAsDataURL(file);
      myReader.onloadend = function (e) {
        self.img_url = myReader.result;
      };
    } else {
      this.ewm_file = file;
      this.ewm_name = imgName + '.' + suffix;
      myReader.readAsDataURL(file);
      myReader.onloadend = function (e) {
        self.ewm_url = myReader.result;
      };
    }
  }

  submitForm() {
    if (!this.img_file) {
      this.hasUploadImg = false;
      return;
    }
    this.homeConfigService.addHomeConfig(this.addHomeConfig).subscribe(
      res => {
        if (res.code === 2000) {
          if (!!this.ewm_file) {
            this.adOssUploadService.getSignature('/v1/common/setting_ewm/' + res.data.id + '?callback=true').subscribe(
              res1 => {
                this.adOssUploadService.fileUpload(this.ewm_file, res1.data, this.ewm_name, 'weichat_ewm').subscribe(
                  res2 => {
                    if (res2.status === 200) {
                      this.uploadLogo(res.data.id);
                    } else {
                      this.type = 'error';
                      this.message = '图片上传失败';
                      this.isShow = true;
                    }
                  }
                );
              }
            );
          } else {
            this.uploadLogo(res.data.id);
          }
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  uploadLogo(id: number) {
    this.adOssUploadService.getSignature('/v1/common/setting_logo/' + id + '?callback=true').subscribe(
      res1 => {
        this.adOssUploadService.fileUpload(this.img_file, res1.data, this.img_name, 'site_logo').subscribe(
          res2 => {
            if (res2.status === 200) {
              this.router.navigate(['home-config/config-list']);
            } else {
              this.type = 'error';
              this.message = '图片上传失败';
              this.isShow = true;
            }
          }
        );
      }
    );
  }
}
